var fs = require('fs');
var mysql = require('mysql');
var superagent = require('superagent');

var msyqlConfig = require('./config').config.mysqlOptions;

let start = 0;
let end = 10000;
let limit = 1;
let offset = start;
let countFinished = 0;

function update() {
    let connection1 = mysql.createConnection(msyqlConfig);
    let query1 = `select * from topic order by id limit ${limit} offset ${offset}`;
    connection1.query(query1, function (err, res) {
        if (err) {
            console.log('query1 error');

        } else {
            console.log('query1 success');
            console.log();

            let list = res;
            if (list.length > 0) {
                let item = list[0];
                let topicId = item.id;
                let url = `http://www.zhihu.com/api/v3/topics/${topicId}`;
                superagent.get(url).end(function (err, res) {
                    if (err) {
                        console.log('http error');
                        setTimeout(() => {
                            update();
                        }, 2000);
                    } else {
                        let text = res.text;
                        let o = JSON.parse(text);

                        let followers_count = o.followers_count;
                        let questions_count = o.questions_count;
                        let name = o.name;

                        let connection2 = mysql.createConnection(msyqlConfig);
                        let query2 = `insert ignore into topic_data(id,follower_count,question_count) values (${topicId},${followers_count},${questions_count})`;
                        connection2.query(query2, function (err, res) {
                            if (err) {
                                // 
                            } else {
                                console.log('query2 success');
                                console.log(res);
                                console.log(name);
                                console.log(followers_count);
                                console.log(questions_count);

                                offset += 1;
                                countFinished += 1;
                                console.log('已完成: ' + countFinished);
                                console.log('offset: ' + offset);


                                console.log();
                                if (offset < end) {
                                    update();
                                } else {
                                    console.log('end');

                                }
                            }
                        });
                        connection2.end();
                    }
                })
            } else {
                console.log(null);
            }
        }
    });
    connection1.end();
}
update();